package com.project.poetry.service.service.impl;

import com.project.poetry.service.entity.dto.money.MoneyLoanDTO;
import com.project.poetry.service.entity.dto.money.MoneyLoanOutDTO;
import com.project.poetry.service.service.MoneyRateService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author mengxj
 */
@Service
public class MoneyRateServiceImpl implements MoneyRateService {

    @Override
    public MoneyLoanOutDTO housingLoan(MoneyLoanDTO moneyLoan) {
        BigDecimal monthRate = BigDecimal.valueOf(moneyLoan.getYearRate());
        monthRate = monthRate.divide(BigDecimal.valueOf(100 * 12), 10, BigDecimal.ROUND_HALF_UP);
        int totalMonth = moneyLoan.getLoanYear() * 12;
        BigDecimal loanPrice = BigDecimal.valueOf(moneyLoan.getLoanPrice());
        MoneyLoanOutDTO res = new MoneyLoanOutDTO();
        List<MoneyLoanOutDTO.OneTimeRepayment> repaymentList = new ArrayList<>();
        res.setRepaymentList(repaymentList);
        res.setTotalRepaymentTime(totalMonth);
        BigDecimal eqPrincipalTotal = new BigDecimal("0");
        BigDecimal oneMonth = loanPrice.divide(BigDecimal.valueOf(totalMonth), 2, BigDecimal.ROUND_HALF_UP);
        BigDecimal eqPrincipalInterestTotal = new BigDecimal("0");
        BigDecimal one = BigDecimal.valueOf(1);
        BigDecimal powRes = monthRate.add(one).pow(totalMonth);
        BigDecimal denominator = loanPrice.multiply(monthRate).multiply(powRes);
        BigDecimal oneTimeEqPrincipalInterestPrice = denominator.divide(powRes.subtract(one), 2, BigDecimal.ROUND_HALF_UP);
        String oneTimeEqPrincipalInterestPriceStr = oneTimeEqPrincipalInterestPrice.toString();
        for(int i = 0; i < totalMonth; i ++){
            BigDecimal oneTimeEqPrincipalPrice = oneMonth.add(loanPrice.subtract(oneMonth.multiply(BigDecimal.valueOf(i))).multiply(monthRate));
            String oneTimeEqPrincipalPriceStr = this.getFormatBigDecimal(oneTimeEqPrincipalPrice).toString();
            MoneyLoanOutDTO.OneTimeRepayment oneTimeRepayment = new MoneyLoanOutDTO.OneTimeRepayment();
            repaymentList.add(oneTimeRepayment);
            oneTimeRepayment.setTime(i + 1);
            oneTimeRepayment.setEqPrincipal(oneTimeEqPrincipalPriceStr);
            oneTimeRepayment.setEqPrincipalInterest(oneTimeEqPrincipalInterestPriceStr);
            oneTimeRepayment.setDifference(this.getFormatBigDecimal(oneTimeEqPrincipalPrice.subtract(oneTimeEqPrincipalInterestPrice)).toString());
            eqPrincipalTotal = eqPrincipalTotal.add(oneTimeEqPrincipalPrice);
            eqPrincipalInterestTotal = eqPrincipalInterestTotal.add(oneTimeEqPrincipalInterestPrice);
        }
        res.setTotalEqPrincipalPrice(eqPrincipalTotal.doubleValue());
        res.setTotalEqPrincipalInterestPrice(this.getFormatBigDecimal(eqPrincipalInterestTotal).doubleValue());
        res.setTotalDifference(this.getFormatBigDecimal(eqPrincipalTotal.subtract(eqPrincipalInterestTotal)).doubleValue());
        return res;
    }

    private BigDecimal getFormatBigDecimal(BigDecimal decimal){
        return decimal.setScale(2, BigDecimal.ROUND_HALF_UP);
    }
}